package com.GraduationDesign.backend.blImpl.user;

import com.GraduationDesign.backend.bl.user.AccountService;
import com.GraduationDesign.backend.data.user.AccountMapper;
import com.GraduationDesign.backend.enums.UserType;
import com.GraduationDesign.backend.po.User;
import com.GraduationDesign.backend.vo.ResponseVO;
import com.GraduationDesign.backend.vo.UserFormVO;
import com.GraduationDesign.backend.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.sql.SQLOutput;

/**
 * @author: wzx
 * @date: 2022/2/27
 */
@Service
@CacheConfig(cacheNames = "user")
public class AccountServiceImpl implements AccountService {

    private final static String ACCOUNT_EXIST = "账号已存在";
    private final static String UPDATE_ERROR = "修改失败";
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public ResponseVO registerAccount(UserVO userVO) {

        if (userVO==null) {
            System.out.println("Failed input");
            return ResponseVO.buildSuccess();
        }//防御式编程

        User user = new User();
        BeanUtils.copyProperties(userVO,user);
        ///属性复制
        User temp=accountMapper.getAccountByEmail(userVO.getEmail());
        System.out.println(temp);
        if(temp!=null){
            return ResponseVO.buildFailure(ACCOUNT_EXIST);
        }
        try {
            UserType[] arr=UserType.values();
            user.setUserType(arr[userVO.getUserType()]);
            accountMapper.createNewAccount(user);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return ResponseVO.buildFailure(ACCOUNT_EXIST);
        }
        return ResponseVO.buildSuccess();
    }
    @Override
    public User login(UserFormVO userFormVO) {
        User user = accountMapper.getAccountByEmail(userFormVO.getEmail());
        if (null == user || !user.getPassword().equals(userFormVO.getPassword())) {
            return null;
        }
        return user;
    }

    @Override
    @Cacheable(key = "#userId",unless = "#result==null ")
    public User getUserInfo(int userId) {
        User user = accountMapper.getAccountById(userId);
        if (user == null) {
            return null;
        }
        return user;
    }

    @Override
    @CachePut(key = "#user.userId")
    public ResponseVO updateUserInfo(int userId, String password, String username, String phonenumber) {
        try {
            accountMapper.updateAccount(userId, password, username, phonenumber);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return ResponseVO.buildFailure(UPDATE_ERROR);
        }
        return ResponseVO.buildSuccess(true);
    }

    @Override
    @CacheEvict(key ="#userId")
    public ResponseVO deleteUser(int userId) {
        User user=accountMapper.getAccountById(userId);
        if(user!=null){
            accountMapper.deleteUser(userId);
            return ResponseVO.buildSuccess("删除成功");
        }
        return ResponseVO.buildFailure("用户不存在");
    }


}
